var express = require('express')
var router = express.Router()
const { getALL, getById, exec } = require('../db')

/* GET users listing. */
router.get('/', async (req, res) => {
  // 一编写sql语句
  let sql = 'select * from gg_user'
  // 二执行sql语句,获得结果
  const users = await getALL(sql)
  // 三返回数据
  res.send(users)
})

/* 获取普通用户信息 commonuser AND user_other="1" */
router.get('/commonuser', async (req, res) => {
  //解析请求体,获得当前页和每页记录数
  const { current, pageSize } = req.query
  //执行sql语句获取查看所有的记录数
  let sql1 = `select count(*) as total from gg_user where user_type="common"`
  let { total } = await getById(sql1)
  //定义偏移量,从0开始查询
  let offset = (current - 1) * pageSize
  //操作数据库,降序排列,根据偏移进行分页
  let sql = `select * from gg_user where user_type="common"order by user_id desc limit ${offset},${pageSize}`
  // 二执行sql语句,获得结果
  const data = await getALL(sql)
  // 三返回数据
  res.send({
    code: 201,
    message: '获取普通用户成功!',
    result: {
      total,
      data,
    },
  })
})

/* 添加普通用户 commonuser*/
router.post('/commonuser', async (req, res) => {
  //解析请求体,获得当前页和每页记录数
  console.log(req.body)
  const { username, password } = req.body
  //操作数据库,判断用户名是否已存在
  let sql = `select * from gg_user where username='${username}' `
  const user = await getALL(sql)
  //1. 根据用户名判断用户是否存在
  if (user[0]) {
    //如果用户存在,返回状态
    return res.send({
      code: 422,
      msg: '用户名重复,请重新输入!',
    })
  } else {
    //操作数据库
    let sql1 = `insert into gg_user(username,password) values('${username}','${password}')`
    // 二执行sql语句,获得结果
    const data = await exec(sql1)
    // 三返回数据
    res.send({
      code: 202,
      msg: '添加普通用户成功!',
    })
  }
})

/* 添加普通用户登录状态 commonuser*/
router.post('/commonuser/onlinestatus', async (req, res) => {
  //解析请求体,
  const { user_id, user_onlinestatus } = req.body
  //操作数据库
  let sql = `update  gg_user set user_onlinestatus='${user_onlinestatus}'  where user_id = ${user_id}`
  // 二执行sql语句,获得结果
  const data = await exec(sql)
  // 三返回数据
  res.send({
    code: 203,
    msg: '修改普通用户状态成功!',
  })
})

/* 修改 */
router.post('/commonuser/changeCommUser', async (req, res) => {
  //解析请求体
  const { user_id, username, password } = req.body
  //操作数据库
  let sql1 = `update  gg_user set username ='${username}',password ='${password}' where user_id = ${user_id}`
  // 二执行sql语句,获得结果
  const data = await exec(sql1)
  // 三返回数据
  res.send({
    code: 205,
    msg: '修改普通用户密码成功!',
  })
})

//修改用户状态
router.post('/commonuser/usertype', async (req, res) => {
  const { user_id, user_other } = req.body
  if (user_other == '0') {
    res.send({
      code: 421,
      msg: '用户已被删除无法更改用户类型!',
    })
  } else {
    const usertype = 'traffic'
    //操作数据库
    let sql = `update  gg_user set user_type='${usertype}'  where user_id = ${user_id}`
    // 二执行sql语句,获得结果
    const data = await exec(sql)
    // 三返回数据
    res.send({
      code: 207,
      msg: '升为交管用户成功!',
    })
  }
})

//删除用户
router.post('/commonuser/delete', async (req, res) => {
  const { user_id, user_other } = req.body
  //操作数据库
  let sql = `update  gg_user set user_other='${user_other}'  where user_id = ${user_id}`
  // 二执行sql语句,获得结果
  const data = await exec(sql)
  // 三返回数据
  res.send({
    code: 204,
    msg: '删除用户成功!',
  })
})
//模糊查询
router.get('/commonuser/filterUser', async (req, res) => {
  // 解析请求参数
  const { current, pageSize, filterContent } = req.query
  // 编写sql语句
  let sql = `select count(*) as total from gg_user where  user_type="common" and username like '%${filterContent}%' `
  let { total } = await getById(sql)
  //定义偏移量
  let offset = (current - 1) * pageSize
  sql = `select * from gg_user  where user_type="common" and username like '%${filterContent}%'  limit ${offset},${pageSize}`
  // 返回结果
  const data = await getALL(sql)
  res.send({
    code: 206,
    message: '查询用户成功!',
    result: {
      total,
      data,
    },
  })
})

//====================交管用户===================================
/* 获  */
router.get('/trafficuser', async (req, res) => {
  //解析请求体,获得当前页和每页记录数
  const { current, pageSize } = req.query
  //执行sql语句获取查看所有的记录数
  let sql1 = `select count(*) as total from gg_user where user_type="traffic"`
  let { total } = await getById(sql1)
  //定义偏移量,从0开始查询
  let offset = (current - 1) * pageSize
  //操作数据库,降序排列,根据偏移进行分页
  let sql = `select * from gg_user where user_type="traffic"order by user_id desc limit ${offset},${pageSize}`
  // 二执行sql语句,获得结果
  const data = await getALL(sql)
  // 三返回数据
  res.send({
    code: 201,
    message: '获取交管用户成功!',
    result: {
      total,
      data,
    },
  })
})

/* 添加交管用户 */
router.post('/trafficuser', async (req, res) => {
  //解析请求体,获得当前页和每页记录数
  const { username, password } = req.body
  const traffic = 'traffic'
  //操作数据库,判断用户名是否已存在
  let sql = `select * from gg_user where username='${username}' `
  const user = await getALL(sql)
  //1. 根据用户名判断用户是否存在
  if (user[0]) {
    //如果用户存在,返回状态
    return res.send({
      code: 422,
      msg: '用户名重复,请重新输入!',
    })
  } else {
    //操作数据库
    let sql1 = `insert into gg_user(username,password,user_type) values('${username}','${password}','${traffic}')`
    // 二执行sql语句,获得结果
    const data = await exec(sql1)
    // 三返回数据
    res.send({
      code: 202,
      msg: '添加交管用户成功!',
    })
  }
})

/* 添加普通用户登录状态 commonuser*/
router.post('/trafficuser/onlinestatus', async (req, res) => {
  //解析请求体,
  const { user_id, user_onlinestatus } = req.body
  //操作数据库
  let sql = `update  gg_user set user_onlinestatus='${user_onlinestatus}'  where user_id = ${user_id}`
  // 二执行sql语句,获得结果
  const data = await exec(sql)
  // 三返回数据
  res.send({
    code: 203,
    msg: '修改交管用户状态成功!',
  })
})

/* 修改 */
router.post('/trafficuser/change', async (req, res) => {
  //解析请求体
  const { user_id, username, password } = req.body
  //操作数据库
  let sql1 = `update  gg_user set username ='${username}',password ='${password}' where user_id = ${user_id}`
  // 二执行sql语句,获得结果
  const data = await exec(sql1)
  // 三返回数据
  res.send({
    code: 205,
    msg: '修改交管用户密码成功!',
  })
})

//删除用户
router.post('/trafficuser/delete', async (req, res) => {
  const { user_id, user_other } = req.body
  //操作数据库
  let sql = `update  gg_user set user_other='${user_other}'  where user_id = ${user_id}`
  // 二执行sql语句,获得结果
  const data = await exec(sql)
  // 三返回数据
  res.send({
    code: 204,
    msg: '删除用户成功!',
  })
})
//修改用户状态
router.post('/trafficuser/usertype', async (req, res) => {
  const { user_id, user_other } = req.body
  if (user_other == '0') {
    res.send({
      code: 421,
      msg: '该用户已被删除,无法更改用户类型!',
    })
  } else {
    const usertype = 'common'
    //操作数据库
    let sql = `update  gg_user set user_type='${usertype}'  where user_id = ${user_id}`
    // 二执行sql语句,获得结果
    const data = await exec(sql)
    // 三返回数据
    res.send({
      code: 207,
      msg: '降普通用户成功!',
    })
  }
})
//模糊查询
router.get('/trafficuser/filterUser', async (req, res) => {
  // 解析请求参数
  const { current, pageSize, filterContent } = req.query
  // 编写sql语句
  let sql = `select count(*) as total from gg_user where  user_type="traffic" and username like '%${filterContent}%' `
  let { total } = await getById(sql)
  //定义偏移量
  let offset = (current - 1) * pageSize
  sql = `select * from gg_user  where user_type="traffic" and username like '%${filterContent}%'  limit ${offset},${pageSize}`
  // 返回结果
  const data = await getALL(sql)
  res.send({
    code: 206,
    message: '查询用户成功!',
    result: {
      total,
      data,
    },
  })
})
module.exports = router
